package cn.woochen.mvvmframe.worker

import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import cn.woochen.comlib.config.RetrofitUtil
import cn.woochen.mvvmframe.config.ApiService
import cn.woochen.mvvmframe.util.logcom
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 * 日志上传任务
 *@author chenwuchao
 *@time 2023-4-4 14:05
 */
class UploadLogWorker(appContext: Context,
    params: WorkerParameters) : CoroutineWorker(appContext, params) {


    private val rxApi by lazy {
        RetrofitUtil.getInstance().retrofit?.create(ApiService::class.java)!!
    }

    override suspend fun doWork(): Result {
        return withContext(Dispatchers.IO){
            try {
                uploadLog()
                logcom("UploadLogWorker执行成功")
                Result.success()
            } catch (e: Exception) {
                logcom("UploadLogWorker执行失败：$e")
                Result.failure()
            }
        }
    }

    private suspend fun uploadLog() {
        rxApi.homeBanner()
    }
}